//
//  MyCenterViewController.m
//  AppointmentForCar
//
//  Created by Herron on 16/10/2.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "MyCenterViewController.h"
#import "SettingViewController.h"
#import "OrdersTableViewController.h"
#import "ModifyUserInfoViewController.h"

@interface MyCenterViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@end

@implementation MyCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    NSString *userId = [UserAccountManager shareUserManager].user.userId;
    [NetWork postNetWorkWithUrl:kURLUserInfo parame:@{@"id":userId} successBlock:^(NSDictionary *dic) {
        NSLog(@"dic = %@", dic);
        UserModel *user = [UserModel mj_objectWithKeyValues:dic[@"data"][@"data"]];
        self.nameLabel.text = user.nickName;
        [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl, user.avatarUrl]]];
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:@"网络异常"];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadNickName) name:kNoticeChangeName object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadAvatarImage) name:kNoticeChangeAvatarImage object:nil];
}

- (void)reloadNickName {
    UserModel *user = [UserAccountManager shareUserManager].user;
    self.nameLabel.text = user.nickName;
}

- (void)reloadAvatarImage {
    UserModel *user = [UserAccountManager shareUserManager].user;
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl, user.avatarUrl]]];
}

- (void)viewWillAppear:(BOOL)animated {
    [self.mm_drawerController setShowsShadow:YES];
    [self.mm_drawerController setMaximumLeftDrawerWidth:200];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kNoticeChangeName object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kNoticeChangeAvatarImage object:nil];
}

- (IBAction)historyOrderClick:(id)sender {
    [self.mm_drawerController setMaximumLeftDrawerWidth:kSCREEN_WIDTH];
    [self.mm_drawerController setShowsShadow:NO];
    OrdersTableViewController *vc = [[OrdersTableViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)settingClick:(id)sender {
    [self.mm_drawerController setMaximumLeftDrawerWidth:kSCREEN_WIDTH];
    [self.mm_drawerController setShowsShadow:NO];
    SettingViewController *vc = [[SettingViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (IBAction)userInfoClick:(id)sender {
    [self.mm_drawerController setMaximumLeftDrawerWidth:kSCREEN_WIDTH];
    [self.mm_drawerController setShowsShadow:NO];
    ModifyUserInfoViewController *vc = [[ModifyUserInfoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

@end
